//
//  WBNewFeatureView.swift
//  Weibo
//
//  Created by kobe on 2016/12/25.
//  Copyright © 2016年 kobe. All rights reserved.
//

import UIKit

class WBNewFeatureView: UIView {

    @IBOutlet weak var scrollView: UIScrollView!
    
    
    @IBOutlet weak var enterBtn: UIButton!
    
    @IBOutlet weak var pageController: UIPageControl!
    
    class func newFeatureView() -> WBNewFeatureView {
        let nib = UINib(nibName: "WBNewFeatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    
    override func awakeFromNib() {
        
        //如果使用自动布局设置的界面 从xib记载默认是600x600
        //添加4个图像视图
        let count = 5
        let rect = UIScreen.main.bounds
        for i in 0..<4 {
            let imageName = "new_feature_\(i+1)"
            
            let iv = UIImageView(image: UIImage(named: imageName))
            
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scrollView.addSubview(iv)
            
        }
        
        //指定
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        scrollView.isPagingEnabled = true
        scrollView.bounces = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        //隐藏按钮
        enterBtn.isHidden = true
    }

    @IBAction func enterWeibo() {
        
        
         UIView.animate(withDuration: 0.5, animations: {
            self.alpha = 0
            }) { (_) in
               self.removeFromSuperview()
        }
        
    }
    
}

extension WBNewFeatureView:UIScrollViewDelegate {
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        
        
        let page = scrollView.contentOffset.x / scrollView.bounds.width
        pageController.currentPage = Int(page)
        
        if page == 3 {
            enterBtn.isHidden = false
            
        } else if page == 4 {
            removeFromSuperview()
        } else {
            enterBtn.isHidden = true
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        enterBtn.isHidden = true
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        pageController.isHidden = (page == scrollView.subviews.count)
    }
    
}
